<template>
  <div>
    <!-- 未激活 -->
    <div>
      <div class="inactive" v-if="!inactive">
        <input
          type="text"
          placeholder="写跟帖"
          v-model="textareaComment"
          @focus="hide"
        />
        <div
          class="pinglun"
          @click="$router.push('/MoreComments/' + $route.params.id)"
        >
          <span class="iconfont iconpinglun-"></span>
          <div class="num">123</div>
        </div>
        <span
          class="iconfont iconshoucang"
          :class="{
            choucang: isChoucang,
          }"
          @click="sendStar"
        ></span>
        <span class="iconfont iconfenxiang" @click="showShare = true"></span>
        <van-share-sheet
          v-model="showShare"
          title="立即分享给好友"
          :options="options"
          @select="onSelect"
        />
      </div>
    </div>
    <!-- 激活 -->
    <div class="theActivation" v-if="inactive">
      <!--   @blur="show" -->
      <textarea
        rows="3"
        placeholder="回复: @ 火星网友"
        @blur="show"
        v-focus
        v-model="textareaComment"
      ></textarea>
      <div class="send" @click="sendComments">发送</div>
    </div>
  </div>
</template>

<script>
// 引入了一个 eventBus vue 实例
import eventBus from "../../utils/eventBus";
export default {
  components: {
    eventBus,
  },
  mounted() {
    // 页面挂载完毕, 利用事件总线 eventBus监听评论回复请求
    eventBus.$on("sendComment", (parentId) => {
      this.hide();
      this.parentId = parentId;
    });
  },
  data() {
    return {
      parentId: "",
      inactive: false,

      isChoucang: false,
      showShare: false,
      options: [
        { name: "没有微信", icon: "wechat" },
        { name: "没有微博", icon: "weibo" },
        { name: "不能复制链接", icon: "link" },
        { name: "不能分享海报", icon: "poster" },
        { name: "没有二维码", icon: "qrcode" },
      ],
      textareaComment: "",
    };
  },
  methods: {
    onSelect(option) {
      this.$toast(option.name);
      this.showShare = false;
    },
    hide() {
      this.inactive = true;
    },
    show() {
      setTimeout(() => {
        this.inactive = false;
      }, 50);
    },
    sendComments() {
      this.$axios({
        method: "post",
        url: "/post_comment/" + this.$route.params.id,
        data: { parent_id: this.parentId, content: this.textareaComment },
      }).then((res) => {
        console.log(res.data);
        this.textareaComment = "";
        this.$toast(res.data.message);
        // 发布评论完毕, 应该通知父组件获取新的评论
        this.$emit("reloadComment");
      });
    },
    sendStar() {
      this.$axios({
        url: "/post_star/" + this.$route.params.id,
      }).then((res) => {
        if (res.data.message === "收藏成功") {
          (this.isChoucang = true), this.$toast(res.data.message);
        } else {
          (this.isChoucang = false), this.$toast(res.data.message);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.inactive {
  background-color: #f2f2f2;
  position: fixed;
  bottom: 0;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
  padding: 20 / 360 * 100vw 0;
  input {
    width: 181 / 360 * 100vw;
    height: 30 / 360 * 100vw;
    border-radius: 15 / 360 * 100vw;
    box-sizing: border-box;
    padding: 0 10 / 360 * 100vw;
  }
  .pinglun {
    position: relative;
    .iconpinglun- {
      font-size: 23 / 360 * 100vw;
    }
    .num {
      position: absolute;
      top: -10 / 360 * 100vw;
      background-color: red;
      color: #fff;
      padding: 0 5 / 360 * 100vw;
      font-size: 16 / 360 * 100vw;
      border-radius: 10 / 360 * 100vw;
    }
  }
  .iconfont {
    font-size: 23 / 360 * 100vw;
  }
  .choucang {
    color: red;
  }
}
.theActivation {
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 5 / 360 * 100vw 20 / 360 * 100vw;
  background-color: #f2f2f2;
  display: flex;
  align-items: flex-end;

  textarea {
    outline: none;
    border: none;
    width: 260 / 360 * 100vw;
    background-color: #d7d7d7;
    border-radius: 10 / 360 * 100vw;
    padding: 10 / 360 * 100vw;
    box-sizing: border-box;
    margin-right: 10 / 360 * 100vw;
    resize: none;
  }
  .send {
    height: 20 / 360 * 100vw;
    line-height: 20 / 360 * 100vw;
    color: #fff;
    background-color: red;
    padding: 0 10 / 360 * 100vw;
    border-radius: 10 / 360 * 100vw;
  }
}
</style>